package com.yonyou.pmclouds.businessobject.util;

import java.util.*;

/**
 * 查询使用in拼接时，超过1000条数据进行拆分
 * @author zhangzhonghao
 */
public class BulkDataSplitUtils {

    public static <T> List<List<T>> getSplitDataList(List<T> list){
        List<List<T>> splitDataList = new ArrayList<>();
        if(list == null){
            return splitDataList;
        }
        int size = list.size();
        if(size <= 999){
            splitDataList.add(list);
            return splitDataList;
        }
        List<T> newDataList = new ArrayList<>();
        for(int i= 0; i < size; i++){
            if(i != 0 && i % 999 == 0){
                if(newDataList.size() > 0){
                    splitDataList.add(newDataList);
                }
                newDataList = new ArrayList<>();
            }
            newDataList.add(list.get(i));
        }

        return splitDataList;

    }

    public static <E> Set<Set<E>> getSplitDataSet(Set<E> set){
        Set<Set<E>> splitDataSet = new HashSet<>();
        if(set == null){
            return splitDataSet;
        }
        int size = set.size();
        if(size <= 999){
            splitDataSet.add(set);
            return splitDataSet;
        }
        Set<E> newDataSet = new HashSet<>();
        Iterator<E> iterator = set.iterator();
        int i = 0;
        while (iterator.hasNext()){
            if(i != 0 && i % 999 == 0){
                if(newDataSet.size() > 0){
                    splitDataSet.add(newDataSet);
                }
                newDataSet = new HashSet<>();
            }
            newDataSet.add(iterator.next());
            i++;
        }

        return splitDataSet;

    }
}
